﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using yncTech.Common;
using yncTech.BusinessObjects;
using EntitySpaces.Interfaces;

public partial class Admin_Fin_Fin005b : AdminBasePage
{
    decimal lendID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[VLendBoxMetadata.ColumnNames.LendID] != null)
        {
            lendID = decimal.Parse(Request.QueryString[VLendBoxMetadata.ColumnNames.LendID]);
        }

    }


    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }
        GridDataBind(e.NewPageIndex);
    }

    /// <summary>
    /// 只加载状态为"存储中",location 跟shipping from location一致,box状态为close而且不是转移中的box
    /// </summary>
    /// <param name="page"></param>
    void GridDataBind(int page)
    {

        VBoxReceiptsCollection boxes = new VBoxReceiptsCollection();
        boxes.Query.Where(boxes.Query.BoxLocation == LoginUserInfo.CurrentUser.ThirdUnitID &&
            boxes.Query.InTransfer == 0 &&
            boxes.Query.StorageStatus == ConstantValue.StorageStatusCode.InStock &&
            boxes.Query.ReceiptCount > 0 && boxes.Query.StorageStatus == ConstantValue.StorageStatusCode.InStock);

        if (txtBoxNo.Text.Trim().Length > 0)
        {
            boxes.Query.Where(boxes.Query.BoxNo.Like(string.Format("%{0}%", txtBoxNo.Text)));
        }

        pageGrid.DataSource = boxes.Query.LoadDataTable();
        pageGrid.CurrentPage = page;
        pageGrid.DataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GridDataBind(1);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            lbMessage.Text = "";

            FinBox finBox = new FinBox();

            FinLendBox lendBox = new FinLendBox();
            FinLend lend = new FinLend();
            using (esTransactionScope tr = new esTransactionScope())
            {
                foreach (RepeaterItem item in pageGrid.Items)
                {
                    CheckBox chkBox = (CheckBox)item.FindControl("chk_sel");
                    if (chkBox.Checked)
                    {
                        Label lbBox = (Label)item.FindControl("lbBoxID");
                        lendBox.BoxID = decimal.Parse(lbBox.Text);
                        lendBox.LendID = lendID;
                        lendBox.UpdatedBy = lendBox.CreatedBy = LoginUserInfo.CurrentUser.LoginName;
                        lendBox.UpdatedDttm = lendBox.CreatedDttm = DateTime.Now;
                        lendBox.Save();

                        //添加后将box的状态设置为:转移中
                        finBox.LoadByPrimaryKey(lendBox.BoxID.Value);
                        finBox.StorageStatus = ConstantValue.StorageStatusCode.Loan;
                        finBox.Save();
                    }
                }
                tr.Complete();

                WebHelper.CloseDialog(true);

            }

        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }
    }
}